package academic.ui.client.widgets;


import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

import ui.client.GenericClickable;
import ui.client.GenericContainerTag;

//example html
/*
 * <div style="position:relative;width:100%;"><span style="">Announcements</span>
 * <a style="position:absolute;font-size:36px; line-height:10px;margin:0px 4px" title="hide announcements"><strong> - </strong></a></div>
 */
public class ClosableWidget extends Composite {
	
	private Boolean isClosed;
	private GenericClickable closeLink = new GenericClickable("a");
	private GenericContainerTag strong=new GenericContainerTag("strong");
	private String CLOSED_TITLE="Show ";
	private String OPEN_TITLE="Hide ";
	private String CLOSED_ICON=" &#9658; ";
	private String OPEN_ICON=" &#9660; ";
	private String name;
	private Widget contents;

	
	public ClosableWidget(String name,Widget contents,Boolean isClosed) {
		this.isClosed=isClosed;
		this.name=name;
		this.contents=contents;
		toggle();
		GenericContainerTag container = new GenericContainerTag("div");
		GenericContainerTag span = new GenericContainerTag("span",name);
		
		closeLink.setStyleName("closeLink");
		
		closeLink.add(strong);
		closeLink.add(span);
		container.add(closeLink);
		container.add(contents);
		initWidget(container);
	}
	
	public HasClickHandlers getClickable() {
		return closeLink;
	}

	public void toggle() {
		
		if(isClosed)
		{
			strong.getElement().setInnerHTML(CLOSED_ICON);
			closeLink.getElement().setTitle(CLOSED_TITLE+name);
			
		}
		else
		{
			strong.getElement().setInnerHTML(OPEN_ICON);
			closeLink.getElement().setTitle(OPEN_TITLE+name);
		}
		isClosed=!isClosed;
		contents.setVisible(isClosed);
	}

}
